package com.skyable.account.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skyable.account.entity.SysCompanyApply;
import com.skyable.account.entity.SysUser;
import com.skyable.account.service.ICompanyApplyService;
import com.skyable.account.service.IUserService;
import com.skyable.common.common.ResponseResult;
import com.skyable.common.common.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author GaoYuan
 * @title: UserController
 * @projectName spring-cloud-oauth2-demo
 * @description: TODO
 * @date 2021/4/1719:46
 */
@Api(value = "用户管理")
@RestController
@RequestMapping("user")
public class UserController extends BaseController {
    
    @Autowired
    private IUserService userService;
    @Autowired
    private ICompanyApplyService companyApplyService;

    /**
     * 新增用户信息
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "新增用户")
    @PostMapping("/insert")
    public ResponseResult insertRole(@RequestBody SysUser sysUser, HttpServletRequest request) {
        setInsertInfo(sysUser,request);
        ServiceResult<SysUser> serviceResult = userService.insertUser(sysUser,sysUser.getRoleIdList());
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }

        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 更新用户信息
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "更新用户")
    @PutMapping("/update")
    public ResponseResult updateRole(@RequestBody SysUser sysUser, HttpServletRequest request) {
        setUpdateInfo(sysUser,request);
        ServiceResult<SysUser> serviceResult = userService.updateUser(sysUser,sysUser.getRoleIdList());
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }

        return ResponseResult.success(serviceResult.getResult());
    }

    /**
     * 获取用户列表
     * @return
     */
    @ApiOperation(value = "获取用户列表")
    @GetMapping("/view")
    public ResponseResult sysUserList(){
        ServiceResult<List<SysUser>> serviceResult = userService.findUserList();
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(JSONObject.toJSON(serviceResult.getResult()));
    }

    /**
     * 分页查询用户列表
     * @param page
     * @param limit
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "分页查询用户信息")
    @PostMapping("/view/{page}/{limit}")
    public ResponseResult sysUserList(@PathVariable("page")Integer page, @PathVariable("limit")Integer limit, @RequestBody SysUser sysUser){
        ServiceResult<IPage<SysUser>> serviceResult = userService.findUserListByPage(page,limit,sysUser);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult().getRecords());
    }

    /**
     * 根据ID查询用户信息
     * @param userId
     * @return
     */
    @ApiOperation(value = "查询用户信息")
    @GetMapping("/view/{id}")
    public ResponseResult getUserById(@PathVariable("id") Long userId){
        ServiceResult<SysUser> serviceResult = userService.findUserById(userId);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(JSONObject.toJSON(serviceResult.getResult()));
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @ApiOperation(value = "删除用户")
    @DeleteMapping("/delete/{id}")
    public ResponseResult deletePermission(@PathVariable("id") Long userId){
        ServiceResult<Integer> serviceResult = userService.deleteUserById(userId);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }

    @ApiOperation(value = "获取用户申请列表")
    @PostMapping("/apply/view/{page}/{limit}")
    public ResponseResult companyApplyList(@PathVariable("page")Integer page, @PathVariable("limit")Integer limit, @RequestBody SysCompanyApply sysCompanyApply){
        ServiceResult<Page<SysCompanyApply>> serviceResult = companyApplyService.sysCompanyApplyList(sysCompanyApply, page, limit);
        if(!serviceResult.getSuccess()) {
            return ResponseResult.error(serviceResult.getCode(),serviceResult.getMessage());
        }
        return ResponseResult.success(serviceResult.getResult());
    }
}
